// 需求相关 api
import { ajaxService } from '@/utils/request';
import type { DemandNumber, DemandNumberItem } from '../demand/type';

const env = import.meta.env;
// let baseURL = env.VITE_DEMAND_URL;
let baseURL = env.VITE_ORDER_URL;
let flowUrl = env.VITE_FLOW_URL;

// 根据客户id查询历史订单的产品
export function getOrderHistoryProduct(params?: any) {
  return ajaxService({
    url: '/order/history/product',
    method: 'get',
    baseURL,
    params,
  });
}

// 获取需求列表
export interface Demand {
  type: number;
}
export function getDemandList(params: Demand) {
  return ajaxService({
    url: '/order/design/getAll',
    method: 'get', // TODO:  暂时用get方法，等后端改get再改get
    params,
    noToken: false,
    baseURL,
  });
}
/**
 * 表单控件
 */
export interface Form {
  name: string;
  code: string;
  componentType: string;
  props: string;
  required: number;
  span: number;
  uuid: string;
}
/**
 * 表单
 * type：表单类型，1需求表单，2订单表单
 * name：表单名称
 * form：表单控件数据
 * print：打印配置
 */
export interface FormComponent {
  type: number;
  name: string;
  form: Form[];
  print: any;
}
export function postOrderDesignAdd(data: FormComponent) {
  return ajaxService({
    url: '/order/design/add',
    method: 'post',
    data,
    noToken: false,
    baseURL,
  });
}
// 删除需求表单与打印
export interface DemandFormDel {
  id: number;
}
export function deleteOrderDesignDel(params: DemandFormDel) {
  return ajaxService({
    url: '/order/design/del',
    method: 'delete',
    params,
    noToken: false,
    baseURL,
  });
}
// 获取一份需求信息
export interface DemandFormInfo {
  id: number;
}
export function getDemandFromInfo(params: DemandFormInfo) {
  return ajaxService({
    url: '/order/design/get',
    method: 'get',
    params,
    noToken: false,
    baseURL,
  });
}
// 添加控件表单
export interface FormAdd {
  id: number;
  userId: number;
  name: string;
  checked: number;
  form: Form[];
  print: any;
}
export function putOrderDesignMod(data: FormAdd) {
  return ajaxService({
    url: '/order/design/mod',
    method: 'post',
    data,
    noToken: false,
    baseURL,
  });
}

// 复制需求控件表单
export interface DemandFormCopy {
  id: number;
}
export function postCopyOrderDesign(data: DemandFormCopy) {
  return ajaxService({
    url: '/order/design/copy',
    method: 'post',
    data,
    noToken: false,
    baseURL,
  });
}

// 需求模板数据类型，后端字段尚未完善
export interface DemandTemplate {
  type: number; // 1需求2订单
  name: string;
  status: number;
  explain: string; // 单据说明
  icon: string;
  orderDesignId: number;
}
// 添加需求模板
export function postDemandTemplate(data: DemandTemplate) {
  return ajaxService({
    url: '/order/template/add',
    method: 'post',
    data,
    noToken: false,
    baseURL,
  });
}

export interface DemandTemplateDel {
  id: number;
}
/**
 * 删除需求模板
 * @param data
 * @returns
 */
export function deleteDemandTemplate(params: DemandTemplateDel) {
  return ajaxService({
    url: '/order/template/del',
    method: 'delete',
    params,
    noToken: false,
    baseURL,
  });
}

export interface DemandTemplateUpdate {
  id?: number;
  type: number; // 1需求2订单
  name: string;
  status: number;
  explain: string; // 单据说明
  icon: string;
  orderDesignId: number;
}

/**
 * 修改需求模板
 * @param data k
 * @returns
 */
export function putDemandTemplate(data: DemandTemplateUpdate) {
  return ajaxService({
    url: '/order/template/mod',
    method: 'post',
    data,
    noToken: false,
    baseURL,
  });
}

export interface DemandTemplateList {
  type: number;
}
/**
 * 获取需求模板列表
 * @param data
 * @returns
 */
export function getDemandTemplateList(params: DemandTemplateList) {
  return ajaxService({
    url: '/order/template/getAll',
    method: 'get',
    params,
    noToken: false,
    baseURL,
  });
}
export interface DemandTemplateInfo {
  id: number;
}
/**
 * 获取一份需求模板信息
 * @param data
 * @returns
 */
export function getDemandTemplateInfo(params: DemandTemplateInfo) {
  return ajaxService({
    url: '/order/template/get',
    method: 'get',
    params,
    noToken: false,
    baseURL,
  });
}
export interface StatusOpen {
  id: number;
}
/**
 * 需求模板状态开启，后端做了自动关闭其他模板状态
 * @param data
 * @returns
 */
export function postDemandTemplateStatus(data: StatusOpen) {
  return ajaxService({
    url: '/order/template/statusOpen',
    method: 'post',
    data,
    noToken: false,
    baseURL,
  });
}
/**
 * 订单编号设置
 * @param data
 * @returns
 */
export function postOrderNumberSetting(data: DemandNumber) {
  return ajaxService({
    url: '/order/numSet',
    method: 'post',
    data,
    noToken: false,
    baseURL,
  });
}
/**
 * 获取订单编号设置
 */
export function getOrderNumberSetting() {
  return ajaxService({
    url: '/order/numSet',
    method: 'get',
    noToken: false,
    baseURL,
  });
}
/**
 * 添加审批流程
 */
export function postBasisFlow(data: any) {
  return ajaxService({
    url: '/basis/flow',
    method: 'post',
    data,
    noToken: false,
    baseURL: flowUrl,
  });
}
interface BasisFlowParams {
  id: number;
}
/**
 * 获取审批流程
 */
export function getBasisFlow(data: BasisFlowParams) {
  return ajaxService({
    url: '/basis/flow',
    method: 'get',
    params: data,
    noToken: false,
    baseURL: flowUrl,
  });
}
/**
 * 编辑审批流程
 */
export function putBasisFlow(data: any) {
  return ajaxService({
    url: '/basis/flow',
    method: 'put',
    data,
    noToken: false,
    baseURL: flowUrl,
  });
}
